package com.sihai.ehr.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.sihai.core.beans.BeanInject;
import com.sihai.core.utils.UserContext;
import com.sihai.core.web.struts2.BaseAction;
import com.sihai.ehr.model.biz.Auditing;
import com.sihai.ehr.model.biz.AuditingBase;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.biz.EmployeeinfoBase;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.service.EmployeeService;
import com.sihai.ehr.service.UserService;

public abstract class EHRAction extends BaseAction {

	private static final long serialVersionUID = -3869191100353150470L;

    @BeanInject
    private UserService userService;
    @BeanInject
    private EmployeeService employeeService;

	protected Long organizationId;
	protected String listAction;
	protected String listTitle;
    protected String currentAuditType;
	protected String currentAuditAction;
	/** 是否显示组织子组织架构联动列表 */
	protected boolean showChildOrg = false;
    protected String action_tag = this.getClass().getSimpleName();

    protected static final String EXPORT_SUCCESS = "export_success";

    protected Long currentAuditingId = 0L;	//当前审核编号
    protected Long currentAuditerUserId = 0L;	//当前审核用户编号
    protected Long currentAuditingDutyId = 0L;	//当前审核职务
    protected boolean lastAuditer = false;
    
	public User getCurrentUser() {
        return userService.getUser(getCurrentUserId());
	}

	public Long getCurrentUserId() {
		return UserContext.getOperatorId();
	}

	public String getQueryString() {
		HttpServletRequest request = ServletActionContext.getRequest();
		// ?page=1&a=1&b=2
		String url = request.getQueryString();
		if (StringUtils.isNotBlank(url)) {
			url = url.replaceAll("page=(\\d)+&", "");
		}
		return StringUtils.trimToEmpty(url);
	}

    //检查当前用户是否可以审核#start
    protected boolean canAudit = false;
    protected void checkCanAudit(EmployeeinfoBase employee, List<? extends AuditingBase> audits){
        try {
            EmployeeinfoWork work = employeeService.getEmplyeeinfoWorkByBaseId(employee.getId());
            checkCanAudit(work.getDuty(), audits);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //duty：审核对象的职务, audits：当前审核流程
    public void checkCanAudit(Duty baseDuty, List<? extends AuditingBase> audits){
        if(audits == null || audits.isEmpty()){
            return;
        }
        User currentUser = getCurrentUser();
        if(currentUser == null || currentUser.getId() == null || currentUser.getId() <= 0){
            return;
        }

        try {
        	int ic = 0;	//计数器
            for(AuditingBase audit : audits){
                if(audit.getStatus() == AuditStatus.wait_audit){
                    Auditing auditNode = audit.getAuditing();
                    //1-指定人审核（employee） 2-指定职务审核（duty） 3-上级领导审核  4-上上级领导审核
                    switch (auditNode.getIndicator()){
                        case 1:
                            long eid = auditNode.getEmployee().getId().longValue();
                            if(eid == currentUser.getWork().getBaseId().getId().longValue()){
                                canAudit = true;
                                lastAuditer = ic == audits.size() - 1 ? true : false;
                                currentAuditingId = audit.getId();
                                currentAuditerUserId = currentUser.getWork().getBaseId().getId();
                                //ＢＵＧ为什么审核配置里DUTY为空
                                //currentAuditingDutyId = audit.getAuditing().getDuty().getId();
                            }
                            return;
                        case 2:
                            if(auditNode.getDuty().getId().longValue() == currentUser.getWork().getDuty().getId().longValue()){
                                canAudit = true;
                                lastAuditer = ic == audits.size() - 1 ? true : false;
                                currentAuditingId = audit.getId();
                                currentAuditerUserId = currentUser.getWork().getBaseId().getId();
                                //currentAuditingDutyId = audit.getAuditing().getDuty().getId();
                            }
                            return;
                        case 3:
                        case 4:
                            Duty duty = userService.findUpDuty(baseDuty, audits, audit); //查找上级或上上级
                            if(duty != null && duty.getId().longValue() == currentUser.getWork().getDuty().getId().longValue()){
                                canAudit = true;
                                lastAuditer = ic == audits.size() - 1 ? true : false;
                                currentAuditingId = audit.getId();
                                currentAuditerUserId = currentUser.getWork().getBaseId().getId();
                                //currentAuditingDutyId = audit.getAuditing().getDuty().getId();
                            }
                            return;
                    }
                    return;
                }
                ic++;
            }
        } catch (Exception e) {
            e.printStackTrace();
            canAudit = false;
        }
    }
    //检查当前用户是否可以审核#end

	public Long getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(Long organizationId) {
		this.organizationId = organizationId;
	}

	public String getListAction() {
		return listAction;
	}

	public void setListAction(String listAction) {
		this.listAction = listAction;
	}

	public String getListTitle() {
		return listTitle;
	}

	public void setListTitle(String listTitle) {
		this.listTitle = listTitle;
	}

	public boolean isShowChildOrg() {
		return showChildOrg;
	}

	public void setShowChildOrg(boolean showChildOrg) {
		this.showChildOrg = showChildOrg;
	}

    public String getCurrentAuditType() {
        return currentAuditType;
    }

    public void setCurrentAuditType(String currentAuditType) {
        this.currentAuditType = currentAuditType;
    }

    public String getCurrentAuditAction() {
        return currentAuditAction;
    }

    public void setCurrentAuditAction(String currentAuditAction) {
        this.currentAuditAction = currentAuditAction;
    }

    public String getAction_tag() {
        return action_tag;
    }

    public boolean isCanAudit() {
        return canAudit;
    }
    
    public Long getCurrentAuditingId() {
		return currentAuditingId;
	}
    
    public void setCurrentAuditingId(Long currentAuditingId) {
		this.currentAuditingId = currentAuditingId;
	}
    
    public Long getCurrentAuditerUserId() {
		return currentAuditerUserId;
	}
    
    public void setCurrentAuditerUserId(Long currentAuditerUserId) {
		this.currentAuditerUserId = currentAuditerUserId;
	}
    
    public Long getCurrentAuditingDutyId() {
		return currentAuditingDutyId;
	}
    
    public void setCurrentAuditingDutyId(Long currentAuditingDutyId) {
		this.currentAuditingDutyId = currentAuditingDutyId;
	}
    
    public boolean isLastAuditer() {
		return lastAuditer;
	}
    
    public void setLastAuditer(boolean lastAuditer) {
		this.lastAuditer = lastAuditer;
	}
}
